package cloud.banson.emojilizer.ui.home

import android.net.Uri
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import cloud.banson.emojilizer.R
import cloud.banson.emojilizer.ui.theme.*
import com.google.accompanist.insets.navigationBarsPadding
import com.google.accompanist.insets.statusBarsPadding

@Composable
fun HomeScreen(
    fetchResponse: () -> Unit,
    selectImage: () -> Unit,
    saveToGallery: () -> Unit,
    invalidateResult: () -> Unit,
    originImgUri: Uri?,
    resultImgUri: Uri?,
    scaffoldState: ScaffoldState,
) {
    var uploadCardExpanded by rememberSaveable { mutableStateOf(false) }
    var getResultCardExpanded by rememberSaveable { mutableStateOf(false) }
    var fetchNewImgPermitted by rememberSaveable { mutableStateOf(false) }

    val onUploadCardClicked: (Boolean) -> Unit = { isArrow ->
        if (!getResultCardExpanded) {
            invalidateResult()
            fetchNewImgPermitted = true
            selectImage()
        }
        uploadCardExpanded = true
        getResultCardExpanded = false
    }

    val onResultCardClicked: (Boolean) -> Unit = { isArrow ->
        if (!getResultCardExpanded) {
            uploadCardExpanded = false
            getResultCardExpanded = true
        }
        if (fetchNewImgPermitted) {
            fetchResponse()
            fetchNewImgPermitted = false
        }
    }
    Scaffold(
        topBar = { AppBar() },
        scaffoldState = scaffoldState,
        snackbarHost = {
            SnackbarHost(it) { data ->
                Snackbar(
                    backgroundColor = Color.DarkGray,
                    contentColor = CyanA100,
                    snackbarData = data,
                )
            }
        }
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .statusBarsPadding()
                .navigationBarsPadding(bottom = false)
                .background(Blue50),
            contentAlignment = Alignment.Center
        ) {
            Column(modifier = Modifier.padding(4.dp)) {
                TouchableCard(
                    name = stringResource(R.string.upload_your_photo),
                    image = originImgUri,
                    expanded = uploadCardExpanded,
                    onClick = onUploadCardClicked,
                    colorBackground = Blue100,
                )
                TouchableCard(
                    name = stringResource(R.string.get_emojilized),
                    image = resultImgUri,
                    expanded = getResultCardExpanded,
                    onClick = onResultCardClicked,
                    colorBackground = Blue300,
                )
                SimpleCard(onClick = { saveToGallery() })
            }
        }
    }
}

@Composable
private fun AppBar() {
    TopAppBar {
        Text(
            text = stringResource(id = R.string.app_name),
            modifier = Modifier.padding(start = 8.dp, top = 2.dp, bottom = 2.dp),
            style = MaterialTheme.typography.h6.copy(
                fontWeight = FontWeight.ExtraBold
            ),
        )
    }
}

@Composable
@Preview(name = "HomeScreen", showBackground = true, widthDp = 360, heightDp = 720)
private fun HomeScreenPreview() {
    EmojilizerTheme() {
        HomeScreen(
            fetchResponse = {},
            selectImage = {},
            saveToGallery = {},
            invalidateResult = {},
            originImgUri = null,
            resultImgUri = null,
            scaffoldState = rememberScaffoldState(),
        )
    }
}